package com.qianying.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.CustomerMaterial;
import com.qianying.service.CustomerMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 客户材料管理控制器
 */
@RestController
@RequestMapping("/admin/customerMaterial")
@Tag(name = "客户材料管理", description = "客户材料表的增删改查接口")
public class CustomerMaterialController {

    @Autowired
    private CustomerMaterialService customerMaterialService;

    /**
     * 分页查询列表
     */
    @GetMapping("/getPage")
    @Operation(
            summary = "分页查询客户材料列表",
            description = "支持按客户名称模糊查询，返回分页数据",
            parameters = {
                    @Parameter(name = "current", description = "页码（默认1）", example = "1"),
                    @Parameter(name = "size", description = "每页条数（默认10）", example = "10"),
                    @Parameter(name = "customer", description = "客户名称（可选）", example = "斐乐")
            }
    )
    public CommonResult<Page<CustomerMaterial>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String customer) {
        Page<CustomerMaterial> page = new Page<>(current, size);
        return customerMaterialService.getPage(page, customer);
    }

    /**
     * 根据ID查询详情
     */
    @GetMapping("/getById")
    @Operation(
            summary = "查询客户材料详情",
            description = "通过ID获取客户材料的完整信息",
            parameters = {
                    @Parameter(name = "id", description = "记录ID", required = true, example = "1")
            }
    )
    public CommonResult<CustomerMaterial> getById(
            @RequestParam Long id) {
        return customerMaterialService.getById(id);
    }

    /**
     * 新增记录
     */
    @PostMapping("/saveMaterial")
    @Operation(
            summary = "新增客户材料记录",
            description = "创建新的客户材料关联记录，客户名称和材料表名称为必填项",
            parameters = {
                    @Parameter(name = "customerId", description = "客户Id", required = true, example = "斐乐"),
                    @Parameter(name = "materialTableName", description = "常用材料表名称", required = true, example = "斐乐材料表"),
                    @Parameter(name = "accessPath", description = "访问路径", example = "http://domain.com/uploads/fila/20250803/xxx.xlsx"),
                    @Parameter(name = "remark", description = "备注", example = "夏季材料清单"),
                    @Parameter(name = "updateTime", description = "更新日期", example = "2025-08-03")
            }
    )
    public CommonResult<?> saveMaterial(
            @RequestBody CustomerMaterial material) {
        return customerMaterialService.saveMaterial(material);
    }

    /**
     * 更新记录
     */
    @PostMapping("/updateMaterial")
    @Operation(
            summary = "更新客户材料记录",
            description = "修改已有客户材料关联记录，必须包含ID",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1"),
                    @Parameter(name = "customerId", description = "客户Id", example = "斐乐"),
                    @Parameter(name = "materialTableName", description = "常用材料表名称", example = "斐乐材料表"),
                    @Parameter(name = "accessPath", description = "访问路径", example = "http://domain.com/uploads/fila/20250803/xxx.xlsx"),
                    @Parameter(name = "excelFileName", description = "上传Excel文件名", example = "斐乐材料表_20250803.xlsx"),
                    @Parameter(name = "remark", description = "备注", example = "夏季材料清单更新"),
                    @Parameter(name = "updateTime", description = "更新日期", example = "2025-08-03")
            }
    )
    public CommonResult<?> updateMaterial(
            @RequestBody CustomerMaterial material) {
        return customerMaterialService.updateMaterial(material);
    }

    /**
     * 删除记录
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除客户材料记录",
            description = "通过ID删除指定的客户材料关联记录",
            parameters = {
                    @Parameter(name = "id", description = "记录ID", required = true, example = "1")
            }
    )
    public CommonResult<?> removeById(
            @RequestParam Long id) {
        return customerMaterialService.removeById(id);
    }
}